<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>mvvm</title>
</head>

<body>
  <input id="bookTitle" value="红楼梦" />
  <script>
    class Book {
      constructor(title) {
        this.title = title;
      }
    }
    let book = new Book('红楼梦');// Model

    var viewModel = { display: 'block' };
    Object.defineProperty(viewModel, 'title', {
      get() {
        return book.title;
      },
      set(newTitle) {
        bookTitle.value = book.title = newTitle;
      }
    });
    Object.defineProperty(viewModel, 'display', {
      get() {
        return bookTitle.style.display;
      },
      set(newDisplay) {
        bookTitle.style.display = newDisplay
      }
    });
    viewModel.title = '新红楼';
    bookTitle.onchange = (event) => {
      viewModel.title = event.target.value;
    }
  </script>
</body>

</html>